/*
 *   Licensed to the Apache Software Foundation (ASF) under one
 *   or more contributor license agreements.  See the NOTICE file
 *   distributed with this work for additional information
 *   regarding copyright ownership.  The ASF licenses this file
 *   to you under the Apache License, Version 2.0 (the
 *   "License"); you may not use this file except in compliance
 *   with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing,
 *   software distributed under the License is distributed on an
 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *   KIND, either express or implied.  See the License for the
 *   specific language governing permissions and limitations
 *   under the License.
 *
 */

package org.apache.directory.server.codec.json.operations.search;


import java.lang.reflect.Type;
import java.util.Iterator;

import org.apache.directory.server.codec.json.JsonBuilder;
import org.apache.directory.shared.ldap.entry.AbstractValue;
import org.apache.directory.shared.ldap.entry.DefaultEntryAttribute;
import org.apache.directory.shared.ldap.entry.Value;
import org.apache.directory.shared.ldap.schema.SchemaManager;

import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;


/**
 * 
 * TODO Attribute.
 *
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 */
public class EntryAttributeCodec implements JsonSerializer<DefaultEntryAttribute>,
    JsonDeserializer<DefaultEntryAttribute>
{

    public JsonElement serialize( DefaultEntryAttribute atSrc, Type typeOfSrc, JsonSerializationContext context )
    {
        JsonObject attrJson = new JsonObject();

        attrJson.addProperty( "name", atSrc.getId() );

        JsonArray ja = new JsonArray();
        Iterator<Value<?>> itr = atSrc.iterator();
        while ( itr.hasNext() )
        {
            Value<?> val = itr.next();
            ja.add( JsonBuilder.toJsonTree( val ) );
        }

        attrJson.add( "values", ja );

        return attrJson;
    }


    public DefaultEntryAttribute deserialize( JsonElement element, Type typeOfT, JsonDeserializationContext context )
        throws JsonParseException
    {
        try
        {
            JsonObject json = element.getAsJsonObject();
            
            String name = json.get( "name" ).getAsString();
            
            DefaultEntryAttribute at = null;
            
            SchemaManager sm = JsonBuilder.getSchemaManager();
            
            if( sm != null )
            {
                at = new DefaultEntryAttribute( sm.lookupAttributeTypeRegistry( name ) );
            }
            else
            {
                at = new DefaultEntryAttribute( name );
            }
            
            JsonArray values = json.getAsJsonArray( "values" );
            
            if( values != null )
            {
                Iterator<JsonElement> itr = values.iterator();
                while( itr.hasNext() )
                {
                    JsonElement val = itr.next();
                    at.add( JsonBuilder.fromJson( val, AbstractValue.class ) );
                }
            }
            
            return at;
        }
        catch( Exception e )
        {
            throw new JsonParseException( e );
        }
    }

}
